#-*- coding:utf8 -*-

from app import db
from .. import tools
from ..model import User


def check_user(username, password):
    """
    用户密码校验
    """
    error = 0
    user = get_user_by_name(username)
    print "user:", user
    if user is not None:
        error = tools.check_password(password, user.pwd)
    else:
        error = 9001
    return error

def is_user_exist(username):
    """根据用户名检查用户是否已存在"""
    user = get_user_by_name(username)
    return True if user is None else False

def get_user_by_name(username):
    '''根据用户名获取user对象'''
    user = User.query.filter_by(nam=username).first()
    return user

def add_user(user):
    '''数据库新增用户数据'''
    if is_user_exist(user.nam):
        return 9004 #用户已存在
    else:
        user.inr = tools.get_inr_by_cnt("usr")
        db.session.add(user)
        db.session.commit()
        return 0

def update_user(user):
    '''数据库更新用户信息'''
    pass
    